package it.polimi.meteocal.ejb.invitationmanagement;

import java.util.ArrayList;
import java.util.List;

import it.polimi.meteocal.ejb.eventmanagement.Event;
import it.polimi.meteocal.ejb.eventmanagement.EventDTO;
import it.polimi.meteocal.ejb.invitationmanager.InvitationDTO;
import it.polimi.meteocal.ejb.invitationmanager.InvitationMgr;
import it.polimi.meteocal.ejb.usermanagement.User;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class InvitationMgrBean implements InvitationMgr {

	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;

	private List<User> users;

	@Override
	public void save(InvitationDTO nuovo, List<?> emails) {
		
		for(int i=0;i<emails.size();i++){
			Invitation newI = new Invitation();
			System.out.println("Email estratta al ciclo "+i+": "+emails.get(i));
			User user = new User();
			user = em.find(User.class, emails.get(i));
			//user.setGroups(groups);
			//users.add(user);
			newI.setUser(user);
			newI.setTitle(nuovo.getTitle());
			newI.setState("1");
			newI.setEventid(nuovo.getEventid());
			em.persist(newI);
		}
		
	}
	
	public User trova(String username){
		return em.find(User.class, username);
	}
	
	public User getPrincipalUser(){
		return trova(getPrincipalUsername());
	}
	
	public String getPrincipalUsername(){
		return context.getCallerPrincipal().getName();
	}

	@Override
	public List<?> findByUser() {
		Query q = em.createNamedQuery("Invitation.findUserInvitation"); 
		q.setParameter("email", getPrincipalUser().getEmail());
		System.out.println("num: "+q.getResultList().size());
		return q.getResultList();
	}

	@Override
	public void accept(int invitationid) {
		Invitation invitation = em.find(Invitation.class, invitationid);
		//em.getTransaction().begin();
		invitation.setState("a");
		//em.getTransaction().commit();
		Event event = em.find(Event.class, invitation.getEventid());
		Event myEvent = new Event();
		myEvent.setBegin(event.getBegin());
		myEvent.setDescription(event.getDescription());
		myEvent.setEnd(event.getEnd());
		myEvent.setInsideOutside(event.getInsideOutside());
		myEvent.setName(event.getName());
		myEvent.setPlace(event.getPlace());
		myEvent.setUser(em.find(User.class,context.getCallerPrincipal().getName()));
		em.persist(myEvent);
	}

	@Override
	public void decline(int invitationid) {
		Invitation invitation = em.find(Invitation.class, invitationid);
		//em.getTransaction().begin();
		invitation.setState("d");
		//em.getTransaction().commit();
	}
	
	@Override
	public List<?> findEventInvitation(int eventid){
		Invitation invitation = new Invitation();
		Query q = em.createNamedQuery("Invitation.findEventInvitation"); 
		q.setParameter("eventid", eventid);
		System.out.println("num: "+q.getResultList().size());
		return q.getResultList();
	}
	
}
